/**
 * Created by CHENCO7 on 8/4/2017.
 */
var version = 0;

$(function () {
    $("#categories>button").on("click", function () {
        var typeId = $(this).attr("id");
        getRemoteData(typeId);
    });
    getVersionNumber();
    setInterval(getVersionNumber, 5000);
});


function getRemoteData(typeId) {
    $.ajax({
        type: "GET",
        url: "http://localhost:8080/client/showMerchantsData?typeId=" + typeId,
        dataType: "json"
    }).done(function (data) {
        updateTable(data);
    })
}

function getVersionNumber() {
    $.ajax({
        type: "GET",
        url: "http://zha-ita077-w7:8080/admins/c/status/version",
        dataType: "json"
    }).done(function (data) {
        if (data.version != version) {
            console.log('data' + data.version);
            console.log('version' + version);
            version = data.version;
            getRemoteData("all");
        }
    })
}

function updateTable(data) {
    $("#tbodyId").empty();

    data.forEach(function (item) {
        var tr = $("<tr></tr>");
        $("#tableId").append(tr);
        tr.append('<td><a href="showFoodList?m_id=' + item.id + '&m_name=' + item.name + '"><img src="' + item.avatar + '" style="width:100px; height:100px"></a></td>');
        tr.append('<td><a href="showFoodList?m_id=' + item.id + '&m_name=' + item.name + '">' + item.name + '</a></td>');
        tr.append('<td>' + item.address + '</td>');
    });
}